package com.xunyin.modules.mainmembersuser.rest;

import com.alibaba.fastjson.JSONObject;
import com.xunyin.api.ApiResult;
import com.xunyin.api.XunyinException;
import com.xunyin.enums.ErrorEnum;
import com.xunyin.modules.log.AppLog;
import com.xunyin.modules.mainmembersuser.param.TMainMembersUserParam;
import com.xunyin.modules.membersuser.param.TMembersUserQueryParam;
import com.xunyin.modules.membersuser.service.TMembersUserService;
import com.xunyin.modules.membersuser.service.dto.TMembersUserDto;
import com.xunyin.modules.membersuser.vo.MemberUserTimeLongVo;
import com.xunyin.modules.membersuser.vo.MemberUserVo;
import com.xunyin.modules.membersuser.vo.MembersDiscountVo;
import com.xunyin.modules.store.service.TStoreService;
import com.xunyin.modules.store.vo.TStoreVo;
import com.xunyin.modules.user.domain.SysXbAppUser;
import com.xunyin.modules.user.service.SysXbAppUserService;
import com.xunyin.utils.CommonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 会员用户控制器
 * </p>
 *
 * @author admin
 * @since 2022-10-15
 */
@Slf4j
@RestController
@Api(value = "会员用户", tags = "共享空间:会员用户")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TMainMembersUserController {

    @Value("${config.activity.new_user_free}")
    private Boolean newUserFree;

    private final TMembersUserService tMembersUserService;

    private final SysXbAppUserService sysXbAppUserService;

    private final TStoreService tStoreService;

    /**
     * 新用户是否弹出领取优惠券
     */
    @AppLog(value = "新用户是否弹出领取优惠券", type = 1)
    //@AuthCheck
    @PostMapping("/members-user/getAd")
    @ApiOperation(value = "新用户是否弹出领取优惠券", notes = "新用户是否弹出领取优惠券")
    public ApiResult<Map<String, Object>> getAd(@Validated @RequestBody TMainMembersUserParam param) {
        Map<String, Object> map = new LinkedHashMap<>();
        if (!newUserFree) {
            map.put("newUserFlag", false);
        }
        SysXbAppUser sysXbAppUser = new SysXbAppUser();
        List<SysXbAppUser> list = sysXbAppUserService.selectSysXbAppUserList(null, param.getOpenId());
        if (list != null && list.size() > 0) {
            sysXbAppUser = list.get(0);
        }
        Integer userStatus = sysXbAppUser.getUserStatus();
        if (userStatus == null || userStatus == 0) {
            map.put("newUserFlag", true);
        }
        map.put("newUserFlag", false); // 调试临时加
        return ApiResult.ok(map);
    }

    /**
     * 新用户领取优惠券
     */
    @AppLog(value = "新用户领取优惠券", type = 1)
    //@AuthCheck
    @PostMapping("/members-user/getNewUserCoupons")
    @ApiOperation(value = "新用户领取优惠券", notes = "新用户领取优惠券")
    public ApiResult<String> getNewUserCoupons(@Validated @RequestBody TMainMembersUserParam param) {
        Boolean updateFlag = tMembersUserService.getNewUserCoupons(param.getOpenId());
        if (updateFlag) {
            return ApiResult.ok("领取成功");
        } else {
            return ApiResult.fail(ErrorEnum.E_100018.getErrorMsg());
        }
    }

    @AppLog(value = "获取当前茶室用户可用时长", type = 1)
    //@AuthCheck
    @PostMapping("/getMembersTimelong")
    @ApiOperation(value = "获取当前茶室用户可用时长", notes = "获取当前茶室用户可用时长")
    public ApiResult<List<TMembersUserDto>> getMembersTimelong(@Validated @RequestBody TMainMembersUserParam param) throws IOException {
        if (param.getMainId() == null || param.getMemberUserPhone() == null) {
            throw new XunyinException(ErrorEnum.E_90004.getErrorMsg());
        }
        List<TMembersUserDto> list = tMembersUserService.getMembersTimelong(param.getMainId(), param.getMemberUserPhone());
        if (list == null) {
            throw new XunyinException(ErrorEnum.E_90004.getErrorMsg());
        } else {
            return ApiResult.ok(list);
        }
    }


    @AppLog(value = "查询会员卡列表", type = 1)
    //@AuthCheck
    @PostMapping("/getUserMemberInfo")
    @ApiOperation(value = "查询会员卡列表", notes = "查询会员卡列表")
    public ApiResult<List<MemberUserVo>> getUserMemberInfo(@Validated @RequestBody TMainMembersUserParam param) {
        List<MemberUserVo> list = tMembersUserService.getUserMemberInfo(param.getMemberUserPhone(), param.getAppId());
        if (list == null) {
            throw new XunyinException(ErrorEnum.E_90004.getErrorMsg());
        } else {
            return ApiResult.ok(list);
        }
    }

    /**
     * 查询会员卡使用网点
     */
    @AppLog(value = "查询会员卡使用网点", type = 1)
    //@AuthCheck
    @PostMapping("/getTeaHouseArray")
    @ApiOperation(value = "查询会员卡使用网点", notes = "查询会员卡使用网点")
    public JSONObject getTeaHouseArray(@Validated @RequestBody TMainMembersUserParam param) {
        List<TStoreVo> list = tStoreService.getTeaHouseArray(param.getTeaHouseIdArray());
        if (list == null) {
            throw new XunyinException(ErrorEnum.E_90004.getErrorMsg());
        } else {
            return CommonUtil.successJson(list);
        }


    }

    /**
     * 转让时长并赠送会员
     */
    @AppLog(value = "转让时长并赠送会员", type = 1)
    //@AuthCheck
    @PostMapping("/getTransferTimeAndMember")
    @ApiOperation(value = "转让时长并赠送会员", notes = "转让时长并赠送会员")
    public JSONObject getTransferTimeAndMember(@Validated @RequestBody TMembersUserQueryParam param) {
        Boolean updateFlag = tMembersUserService.getTransferTimeAndMember(param.getAppId(), param.getMainId(),
                param.getTransferTime(), param.getTransPhone(), param.getUserPhone());
        if (updateFlag) {
            return CommonUtil.successJson();
        } else {
            return CommonUtil.errorJson(ErrorEnum.E_90004);
        }
    }

    /**
     * 会员转让记录
     */
    @AppLog(value = "会员转让记录", type = 1)
    //@AuthCheck
    @PostMapping("/getTimelongRecord")
    @ApiOperation(value = "会员转让记录", notes = "会员转让记录")
    public ApiResult<List<MemberUserTimeLongVo>> getTimelongRecord(@Validated @RequestBody TMembersUserQueryParam param) {
        List<MemberUserTimeLongVo> list = tMembersUserService.getTimelongRecord(param);
        ;
        if (list == null) {
            throw new XunyinException(ErrorEnum.E_90004.getErrorMsg());
        } else {
            return ApiResult.ok(list);
        }
    }

    /**
     * 获取会员用户折扣信息
     */
    @AppLog(value = "获取会员用户折扣信息", type = 1)
    //@AuthCheck
    @PostMapping("/getMembersDiscount")
    @ApiOperation(value = "获取会员用户折扣信息", notes = "获取会员用户折扣信息")
    public ApiResult<MembersDiscountVo> getMembersDiscount(@Validated @RequestBody TMembersUserQueryParam param) {
        MembersDiscountVo memberUser = tMembersUserService.getMembersDiscount(param.getMainId(), param.getUserPhone());
        return ApiResult.ok(memberUser);
    }


}
